package com.zh.risk.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.risk.dao.StrategyDao;
import com.zh.risk.entity.RiskMerchTrans;
import com.zh.risk.entity.Strategy;
import com.zh.risk.service.StrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 策略业务接口实现
 * @Author: Liard
 * @Create in: 2020/8/26 10:17
 * @Version: 1.0.1
 */
@Service
public class StrategyServiceImpl extends ServiceImpl<StrategyDao, Strategy> implements StrategyService {

    @Autowired
    private StrategyDao dao;

    @Override
    public IPage<Strategy> getStrategyList(Page<Strategy> page, Strategy param) {
        return dao.selectPage(page, new LambdaQueryWrapper<Strategy>()
                .like(StrUtil.isNotEmpty(param.getName()), Strategy::getName, param.getName()));
    }

    @Override
    public boolean addStrategy(Strategy param) {
        param.setCreateDate(DateUtil.now());
        return save(param);
    }

    @Override
    public boolean modStrategy(Strategy param) {
        return update(param, new LambdaQueryWrapper<Strategy>().eq(Strategy::getId, param.getId()));
    }

    @Override
    public boolean delStrategy(Strategy param) {
        return removeById(param.getId());
    }

    @Override
    public List<RiskMerchTrans> getBindRulers(String id) {
        List<RiskMerchTrans> list = dao.getBindRulers(id);
        list.removeAll(Collections.singleton(null));
        if (CollUtil.isNotEmpty(list))
            list.forEach(item -> {
                item.setTitle(item.getName());
                item.setDirection("left");
            });
        return list;
    }

    @Override
    public List<RiskMerchTrans> getNotBindRulers(String id) {
        List<RiskMerchTrans> list = dao.getNotBindRulers(id);
        list.removeAll(Collections.singleton(null));
        if (CollUtil.isNotEmpty(list))
            list.forEach(item -> {
                item.setTitle(item.getName());
                item.setDirection("right");
            });
        return list;
    }

    @Override
    public int bindRuleStrategy(List<String> ruleIds, String strategyId) {
        int i = dao.delRules(strategyId);
        if (Objects.nonNull(ruleIds))
            return dao.bindRuleStrategy(ruleIds, strategyId);
        return i;
    }
}
